package com.meida.boot.admin.handler;

import com.meida.common.base.client.BasePushClient;
import com.meida.common.enums.AuthStatusEnum;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.UpdateInterceptor;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.common.utils.ApiAssert;
import com.meida.module.admin.provider.service.BaseUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 企业审核成功扩展,审核成功后注册企业账户
 *
 * @author zyf
 */
@Component("comapnyAuthSuccessHandler")
public class ComapnyAuthSuccessHandler implements UpdateInterceptor {

    @Resource
    private BaseUserService baseUserService;

    @Resource
    private WanLeMeSmsHandler wanLeMeSmsHandler;

    @Resource
    private BasePushClient basePushClient;


    /**
     * 更新条件扩展
     *
     * @param cu
     * @return
     */
    @Override
    public void prepare(CriteriaUpdate cu, EntityMap map) {
        Integer auditState = map.getInt("auditState", 1);
        if (auditState.equals(AuthStatusEnum.AUTHSUCCESS.getCode())) {
            String accountName = map.get("accountName");
            ApiAssert.isNotEmpty("accountName不能为空", accountName);
            Long companyId = map.getLong("companyId");
            ApiAssert.isNotEmpty("companyId不能为空", companyId);
            String roleId = map.get("roleId");
            ApiAssert.isNotEmpty("roleId不能为空", roleId);
        }
    }

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params) {
        Integer auditState = params.getInt("auditState", 1);
        String accountName = params.get("accountName");
        if (auditState.equals(AuthStatusEnum.AUTHSUCCESS.getCode())) {
            String password = params.get("password");
            Long managerId = params.getLong("managerId");
            wanLeMeSmsHandler.companyAuthSuccess(accountName, "", accountName, password);
            basePushClient.getService("name1").sendByDeviceToken(managerId, "企业审核", "", "您申请的企业已审核通过", "");
            baseUserService.addUser(params);
        } else {
            baseUserService.deleteUser(accountName);
        }
    }
}
